package com.example.ele_guarantee_hh_pro.trigger;

import com.cloudcc.deveditor.core.CCObject;

import com.cloudcc.deveditor.trigger.TriggerEditor;
import com.example.ele_guarantee_hh_pro.comclass.ImportAccount;
import com.example.ele_guarantee_hh_pro.comclass.SaveAccountTest;

import java.util.List;
import java.util.Map;

/*管户小组*/
public class manage_group extends TriggerEditor {

    /*新增管护权*/
    public void beforeInsert() {
        String YES="是";
        String DFP="待跟进";
        String ghzt_lost="已失效";
        //管户经理
        String fk_ccuser=record_new.get("fk_ccuser")==null?"":record_new.get("fk_ccuser")+"";
        //id
        String id=record_new.get("id")==null?"":record_new.get("id")+"";
        //是否写入共享
        String is_shared=record_new.get("is_shared")==null?"":record_new.get("is_shared")+"";
        //客户id
        String fk_account=record_new.get("fk_account")==null?"":record_new.get("fk_account")+"";
        //统一社会信用代码
        String name=record_new.get("name")==null?"":record_new.get("name")+"";

        if("".equals(name)){
            String code_sql="select invoiceTaxNo from Account where id='"+fk_account+"' and is_deleted='0'";
            List<CCObject> account = this.cqlQuery("Account", code_sql);
            if(account.size()>0){
                String invoiceTaxNo = account.get(0).get("invoiceTaxNo") == null ? "" : account.get(0).get("invoiceTaxNo") + "";
                this.cqlQuery("Account","update manage_group set name='"+invoiceTaxNo+"' where id='"+id+"'");
            }
        }
        //更新客户状态
        CCObject account = new CCObject("Account");
        account.put("id",fk_account);
        List<CCObject> account1 = this.cquery("Account", "id='" + fk_account + "'");
        if(account1.size()>0){
            String now_status= account1.get(0).get("follow_status")==null?"":account1.get(0).get("follow_status")+"";
            if(now_status.equals("待分配")){
                account.put("follow_status",DFP);
            }
        }

        //验证有其他管户小组
        String sql_manager="select * from manage_group " +
                "where ghzt!='"+ghzt_lost+"' " +
                "and is_deleted='0' " +
                "and fk_account='"+fk_account+"'";
        List<CCObject> manage_group = this.cqlQuery("manage_group", sql_manager);
        if(manage_group.size()==0){
            account.put("ownerid",fk_ccuser);
        }
        this.update(account);
        if(is_shared.equals(YES)){
            //创建共享对象
            CCObject Account_share = new CCObject("Account",CCObject.IS_SHARED);
            Account_share.put("parentid",fk_account);
            Account_share.put("accesslevel", "Write");
            Account_share.put("rowcause", "Manual");
            Account_share.put("userorgroupid",fk_ccuser);
            this.insert(Account_share);
        }

    }

    /*验证管户经理*/
    public void beforeInsert(Map record_new) {
        String ghzt_continue="已失效";
        //公共资源交易中心
        String fk_psmc=record_new.get("fk_psmc")==null?"":record_new.get("fk_psmc")+"";
        //客户
        String fk_account=record_new.get("fk_account")==null?"":record_new.get("fk_account")+"";


        String check_sql="select id from manage_group " +
                "where fk_psmc='"+fk_psmc+"' " +
                "and is_deleted='0' " +
                "and ghzt!='"+ghzt_continue+"' " +
                "and fk_account='"+fk_account+"'";
        List<CCObject> manage_group_list = this.cqlQuery("manage_group", check_sql);
        if(manage_group_list.size()>0){
            //if(true)trigger.addErrorMessage("当前交易中心已存在管户经理");
        }

    }

    /*变更状态回收权限*/
        public void afterUpdate() throws Exception {

            String YES = "是";
            String ghzt_lost = "已失效";
            String follow_status_dfp = "待分配";
            //管户状态
            String ghzt = record_new.get("ghzt") == null ? "" : record_new.get("ghzt") + "";
            //是否分享
            String is_shared = record_new.get("is_shared") == null ? "" : record_new.get("is_shared") + "";
            //客户id
            String fk_account = record_new.get("fk_account") == null ? "" : record_new.get("fk_account") + "";
            //管户经理
            String fk_ccuser = record_new.get("fk_ccuser") == null ? "" : record_new.get("fk_ccuser") + "";
            String id = record_new.get("id") == null ? "" : record_new.get("id") + "";

            //
            String newlxdz = record_new.get("lxdz") == null ? "" : record_new.get("lxdz") + "";
            String recordold = "";
            try{
                recordold =  record_old.get("lxdz") == null ? "" : record_old.get("lxdz") + "";
            }catch (Exception e){}

            if (ghzt.equals(ghzt_lost)) {
                String sql_manager = "select * from manage_group " +
                        "where ghzt!='" + ghzt_lost + "' " +
                        "and is_deleted='0' " +
                        "and fk_account='" + fk_account + "'";
                if (is_shared.equals(YES)) {
                    this.deleteShareObjectBySql("Account",
                            "parentid='" + fk_account + "' " +
                                    "and userorgroupid='" + fk_ccuser + "' " +
                                    "and accesslevel!='ALL'");
                    //查询该客户是否还有其他管护权,如果没有更新客户状态，使之回退公海池
                    List<CCObject> manage_group_other = this.cqlQuery("manage_group", sql_manager);
                    String sql = "select pool from ccuser where id='" + fk_ccuser + "'";
                    List<CCObject> pool_list = this.cqlQuery("ccuser", sql);
                    String pool = "";
                    if (pool_list.size() > 0) {
                        pool = pool_list.get(0).get("pool") == null ? "" : pool_list.get(0).get("pool") + "";
                    }
                    if (manage_group_other.size() == 0) {
                        CCObject account = new CCObject("Account");
                        account.put("id", fk_account);
                        account.put("follow_status", follow_status_dfp);
                        account.put("ownerid", pool);
                        this.update(account);
                    }
                } else {
                    String sql = "select pool from ccuser where id='" + fk_ccuser + "'";
                    List<CCObject> pool_list = this.cqlQuery("ccuser", sql);
                    if (pool_list.size() > 0) {
                        String pool = pool_list.get(0).get("pool") == null ? "" : pool_list.get(0).get("pool") + "";
                        CCObject account = new CCObject("Account");
                        account.put("id", fk_account);
                        //查询该客户是否还有其他管护权,如果没有更新客户状态，使之回退公海池
                        List<CCObject> manage_group_other = this.cqlQuery("manage_group", sql_manager);
                        if (manage_group_other.size() == 0) {
                            account.put("follow_status", follow_status_dfp);
                            account.put("ownerid", pool);
                            this.update(account);
                        } else {
                            pool = manage_group_other.get(0).get("manage_group_other") + "";

                            String update_ac = "update Account set ownerid='" + pool + "',follow_status='" + follow_status_dfp + "' where id='" + fk_account + "'";
                            this.cqlQuery("Account", update_ac);
                        }

                    }
                }

            }
            if (!newlxdz.equals(recordold)){
                ImportAccount saveAccountTest = new ImportAccount(userInfo);
                String gen = saveAccountTest.gen(newlxdz);
                String[] split = gen.split(",");
                String update  = "update manage_group set dlwzco='"+split[0]+"',dlwzla='"+split[1]+"' where id ='"+id+"' ";
                this.cqlQuery("manage_group",update);
            }

        }


}
